(function (global) {
    /**
     * api
     * @type {getLoginUser}
     */
    const getLoginUser = api.getLoginUser;
    const getArticleById = api.getArticleById;
    const sendComment = api.sendComment;
    const addReply = api.addReply;
    const getCommentsByArticleId = api.getCommentsByArticleId;
    const getArticleZanStateApi = api.getArticleZanState;
    const isCollectedApi = api.articleCollectState;
    const articleCollectApi = api.articleCollect;
    const articleZanApi = api.articleZan;
    const getZanCountApi = api.articleZanCount;
    const articleCountOfUserApi = api.articleCountOfUser;

    const followStateApi = api.followState;
    const cancelFollowApi = api.cancelFollow;
    const followApi = api.follow;

    var apiBase = "http://172.15.120.249:8000";

    var param = document.location.href.split('?')[1];
    var pos = null;
    if (param.indexOf('#') != -1) {
        pos = param.split('#')[1]
        param = param.split('#')[0]
    }


    articleId = param.split('=')[1];  //文章ID
    var commentsVue = new Vue({
        el: '#article',
        data: {
            show: true,
            enableComment: true,
            articleId: articleId,
            article: {},
            comments: [],
            zan: false,  //是否点赞了
            zanCount: 0,
            collect: false,  // 是否收藏该文章了
            readCount: 0,
            followed:false,
            loginUser: {}
        },
        async created() {
            window.setTimeout(() => {
                if (pos) {
                    var t = $("#" + pos).offset().top;

                    $(window).scrollTop(t);//滚动到锚点位置
                }
            }, 1000);
            await this.loadArticle();
            this.initFollow();
            this.initComments()
            // 获取点赞状态
            this.initZan();  // 初始化点赞
            this.initCollect(); // 初始化收藏
            await this.getLoginUser();  //获取当前登录用户
            console.log('user')
            console.log(this.article)
            if (this.loginUser.id == this.article.user.id) {
                this.enableComment = false;
            }
            this.show = true;
            initPic1()
        },
        methods: {
            async getLoginUser() {                  // 获取登陆用户信息
                // 获取当前登录的用户
                var data = await getLoginUser();
                this.loginUser = data.data;
                // 获取用户的文章信息、关注量
                var res = await articleCountOfUser(this.article.user.id)
                this.loginUser.articleCount = res.data;

                this.loginUser.followCount = 100;
                // 是否关注了作者
                var followRes = await followStateApi(this.article.user.id);
                this.followed = followRes.data;

            },
            async initZan() {  //获取文章点赞状态
                var res = await getArticleZanStateApi(articleId);
                if (res.code == 0)
                    this.zan = res.data.zan == "1" ? true : false;
                // 获取点赞数量
                var res2 = await getZanCountApi(articleId);
                if (res2.code == 0)
                    this.zanCount = res2.data;
            },
            async initCollect() {  // 初始化收藏状态
                var res = await isCollectedApi(articleId);
                if (res.code == 0) {
                    this.collect = res.data == "1" ? true : false;
                }
            },
            async zanHandler(e) {  // 处理点赞事件, 不可以取消点赞
                if (this.zan == true)
                    return;
                var res = await articleZanApi({articleId})
                if (res.code == 0) {
                    this.zan = true;
                    this.zanCount++;
                    layer.msg('点赞成功', {
                        icon: 6
                        , time: 1000
                    })
                }
            },
            async collectHandler() {  // 处理点收藏事件 不可以取消收藏（在这里）
                if (this.collect == true)
                    return;
                var res = await articleCollectApi(articleId);
                if(res.code == 0){
                    if (res.code == 0) {
                        this.collect = true;
                        layer.msg('收藏成功', {
                            icon: 6
                            , time: 1000
                        })
                    }
                }
            },
            async initFollow(){
                var res = await followStateApi(this.article.user.id);
                this.followed = res.data;
            },
            async followHandler(){
                if(this.followed){ // 取消关注
                    var res = await cancelFollowApi(this.article.user.id)
                }else{  // 关注
                    var res = await followApi(this.article.user.id)
                }
                this.followed = !this.followed;
            },
            async loadArticle() {
                const articleData = await getArticleById(articleId);
                this.article = articleData.data;
            },
            showTextArea(event,type,target) {  // 展示输入框
                if(type==1){
                    if (!this.enableComment) {
                        layuiUtil.msg.msgWarning('不可评论自己的文章！', 2);
                        return;
                    }
                    $('div.comment-area').addClass("hide")
                    $(event.target).parent().parent().find("div.comment-area").removeClass("hide")
                }else{
                    if (this.loginUser.id == target) {
                        layuiUtil.msg.msgWarning('不可回复自己！', 2);
                        return;
                    }
                    $('div.comment-area').addClass("hide")
                    $(event.target).parent().parent().find("div.comment-area").removeClass("hide")
                }
            },
            async sendComments(event) {  // 发送评论
                var content = $(event.target).parent().prev().val(); // 评论内容
                var articleId = this.articleId;
                var res = await sendComment({articleId, content})
                if (res.code == 0) {
                    this.comments.push(res.data);
                    $('div.comment-area').addClass("hide");
                    // 定位
                    window.setTimeout(() => {
                        var t = $("#comment_pos" + (this.comments.length - 1)).offset().top;
                        $(window).scrollTop(t);//滚动到锚点位置
                    }, 1000)
                    $('div.comment-area textarea').val("");
                }
            },
            async sendReply(commentId, to, index, event) {  // from为当前登录用户，在后台获取
                var content = $(event.target).parent().prev().val();
                // 添加回复
                var res = await addReply({commentId, to, content})
                if (res.code == 0) {
                    if (this.comments[index].replyList == null) {
                        this.comments[index].replyList = [];
                    }
                    this.comments[index].replyList.push(res.data)
                    $('div.comment-area').addClass("hide")
                    $('div.comment-area textarea').val("");
                }
            },
            async initComments() {
                var res = await getCommentsByArticleId({articleId});
                this.comments = res.data;
            }
        }
    })


    $.ajax({
        url: 'http://172.15120.249:8000/sentiment',
        type: 'post',
        data: {
            id: 1,
            content: '自然语言处理是计算机科学领域与人工智能领域中的一个重要方向。所以它与语言学的研究有着密切的联系，但又有重要的区别，自然语言处理并不是一般地研究自然语言。而在于研制能有效地实现自然语言通信的计算机系统，特别是其中的软件系统。因而它是计算机科学的一部分'
        },
        success: function (res) {
            console.log(res)
        }
    })



})(this);

//注意：选项卡 依赖 element 模块，否则无法进行功能性操作
layui.use('element', function () {
    var element = layui.element;

    //…
});


